class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


import collections


class Solution(object):
    def findFrequentTreeSum(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        c = collections.defaultdict(int)

        def search(node):
            left_num = 0
            right_num = 0
            if node.left:
                left_num = search(node.left)
            if node.right:
                right_num = search(node.right)
            now_val = left_num + right_num + node.val
            c[now_val] += 1
            return now_val

        search(root)
        max_value = max(c.values())
        out = []
        for key in c:
            if c[key] == max_value:
                out.append(key)

        return out
